import React from 'react';
import {injectIntl} from './intl';
import PropTypes from 'prop-types';
const basePath = fetch.basePath || '';
import fileTemplate from '../attachment/服务活动指定车辆清单导入模板.xlsx';
import ExcelImport from '../common/ExcelImport';

class FileImport extends React.PureComponent {
    constructor(props) {
        super(props);
        this.state = {
            loading: false,
            importStatus: false
        };
    }
    beforeUpload = () => this.setState({loading: true});

    afterUpload = ({ok, response}) => {
        this.setState({
            loading: false,
            importStatus: this.state.importStatus || ok
        });
        if(ok)
            this.props.onImportOk(response.payload.content);
    }

    render() {
        const description = [
            this.props.intl.formatMessage({
                id: 'fileImport.step2.description1',
                defaultMessage: 'VIN不能为空，且长度为17位'
            }),
            this.props.intl.formatMessage({
                id: 'fileImport.step2.description2',
                defaultMessage: 'VIN不能重复'
            }),
            this.props.intl.formatMessage({
                id: 'fileImport.step2.description3',
                defaultMessage: '责任经销商必须是有效状态的经销商'
            }),
        ];

        return ([
            <ExcelImport
                key="import"
                template={fileTemplate}
                action={`${basePath}/afterSales/api/v1/serviceActivities/import`}
                description={description}
                beforeUpload={this.beforeUpload}
                afterUpload={this.afterUpload}/>
        ]);
    }
}

FileImport.propTypes = {
    intl: PropTypes.object,
    onImportOk: PropTypes.func
};

export default injectIntl(FileImport);
